package org.small.shop.phone.api.process.addr.service;

import java.util.List;

import org.small.shop.phone.api.process.addr.mapper.TblWxAddrMapper;
import org.small.shop.phone.api.process.addr.pojo.TblWxAddr;
import org.small.shop.phone.api.process.addr.pojo.TblWxAddrVo;
import org.small.shop.phone.api.process.openuser.mapper.TblWxOpenUserMapper;
import org.small.shop.phone.api.process.openuser.pojo.TblWxOpenUser;
import org.small.shop.phone.api.utils.AppConstantUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.util.StringUtil;

@Service
public class TblWxAddrServiceImpl implements TblWxAddrService {

	@Autowired
	private TblWxAddrMapper tblWxAddrMapper;

	@Autowired
	private TblWxOpenUserMapper tblWxOpenUserMapper;

	@Override
	public List<TblWxAddrVo> getAllAddrsByUserId(String appId) {

		return tblWxAddrMapper.getAllAddrsByUserId(appId);
	}

	@Override
	@Transactional
	public void setDefaultAddrByAddrId(String appId, Integer addrId) {
		tblWxAddrMapper.updateAllNotDefaultByUserId(appId);
		tblWxAddrMapper.updateIsDefaultByAddrId(addrId);
	}

	@Override
	@Transactional
	public void deleteAddrById(Integer addrId, String appId) {
		
		tblWxAddrMapper.deleteByPrimaryKey(addrId);
	}

	@Override
	public TblWxAddrVo getAddrsByaddrId(Integer addrId) {

		TblWxAddrVo tblWxAddrVo = tblWxAddrMapper.getAddrsByaddrId(addrId);
		if (tblWxAddrVo != null && !StringUtil.isEmpty(tblWxAddrVo.getAdrrAreaidpath())) {
			String areaidpath = tblWxAddrVo.getAdrrAreaidpath();
			String[] areaidpaths = areaidpath.split(AppConstantUtils.SPLIT_STR);
			if (areaidpaths.length == 3) {
				tblWxAddrVo.setProvince(areaidpaths[0]);
				tblWxAddrVo.setCity(areaidpaths[1]);
				tblWxAddrVo.setCounty(areaidpaths[2]);
			} else if (areaidpaths.length == 2) {
				tblWxAddrVo.setProvince(areaidpaths[0]);
				tblWxAddrVo.setCity(areaidpaths[1]);
			} else if (areaidpaths.length == 1) {
				tblWxAddrVo.setProvince(areaidpaths[0]);
			}
			if (!StringUtil.isEmpty(tblWxAddrVo.getAddrUseraddress())
					&& tblWxAddrVo.getAddrUseraddress().lastIndexOf(AppConstantUtils.SPLIT_STR1) > 0) {
				tblWxAddrVo.setAddrUseraddress(tblWxAddrVo.getAddrUseraddress()
						.substring(tblWxAddrVo.getAddrUseraddress().lastIndexOf(AppConstantUtils.SPLIT_STR1) + 1));
			}
		}
		return tblWxAddrVo;
	}

	@Override
	@Transactional
	public void updateAddr(String appId, TblWxAddrVo addrVo) throws Exception {

		TblWxOpenUser openUser = tblWxOpenUserMapper.selectByPrimaryKey(appId);
		if (openUser != null && !StringUtil.isEmpty(openUser.getUserid())) {

			if (addrVo.getAddrIsdefault() != null && addrVo.getAddrIsdefault() == 1) {
				tblWxAddrMapper.updateAllNotDefaultByUserId(appId);
			}

			TblWxAddr tblWxAddr = new TblWxAddr();
			tblWxAddr.setUserId(openUser.getUserid());
			setTblWxAddr(tblWxAddr, addrVo);
			tblWxAddrMapper.updateByPrimaryKeySelective(tblWxAddr);
		} else {
			throw new Exception();
		}
	}

	@Override
	@Transactional
	public void addAddr(String appId, TblWxAddrVo addrVo) throws Exception {

		TblWxOpenUser openUser = tblWxOpenUserMapper.selectByPrimaryKey(appId);
		if (openUser != null && !StringUtil.isEmpty(openUser.getUserid())) {

			if (addrVo.getAddrIsdefault() != null && addrVo.getAddrIsdefault() == 1) {
				tblWxAddrMapper.updateAllNotDefaultByUserId(appId);
			}

			TblWxAddr tblWxAddr = new TblWxAddr();
			tblWxAddr.setUserId(openUser.getUserid());
			setTblWxAddr(tblWxAddr, addrVo);
			tblWxAddrMapper.insertSelective(tblWxAddr);
		} else {
			throw new Exception();
		}
	}

	private void setTblWxAddr(TblWxAddr tblWxAddr, TblWxAddrVo addrVo) {

		// 设置区域ID路径(省Id_市Id_县Id 例如:440000_440100_440106_)
		String adrrAreaidpath = "";
		if (!StringUtil.isEmpty(addrVo.getProvince())) {
			adrrAreaidpath += addrVo.getProvince();
		}
		if (!StringUtil.isEmpty(addrVo.getCity())) {
			adrrAreaidpath += "_" + addrVo.getCity();
			tblWxAddr.setAddrAreaid(Integer.parseInt(addrVo.getCity()));
		}
		if (!StringUtil.isEmpty(addrVo.getCounty())) {
			adrrAreaidpath += "_" + addrVo.getCounty();
		}
		
		tblWxAddr.setAdrrAreaidpath(adrrAreaidpath);
		tblWxAddr.setAddrCreateTime(System.currentTimeMillis());
		tblWxAddr.setAddrStatus(1);
		tblWxAddr.setAddrIsdefault(addrVo.getAddrIsdefault());
		tblWxAddr.setAddrUpdateTime(System.currentTimeMillis());
		tblWxAddr.setAddrUseraddress(addrVo.getAddrUseraddress());
		tblWxAddr.setAdrrUsername(addrVo.getAdrrUsername());
		tblWxAddr.setAdrrUserphone(addrVo.getAdrrUserphone());
		tblWxAddr.setAddrId(addrVo.getAddrId());
	}
}
